//
//  YYLHomeTableViewCell.swift
//  XinlangWeibo
//
//  Created by yangyinglei on 2017/3/31.
//  Copyright © 2017年 yangyinglei. All rights reserved.
//

import UIKit
import SnapKit

class YYLHomeTableViewCell: UITableViewCell {
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
    }

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI() {
        contentView.backgroundColor = UIColor(white: 0.95, alpha: 1)
        
        contentView.addSubview(originalView)
        contentView.addSubview(retWeetView)
        contentView.addSubview(commonView)
        //布局
        originalView.snp.makeConstraints { (make) in
            make.leading.trailing.equalTo(self.contentView)
            make.top.equalTo(self.contentView).offset(8)
        }
        
        retWeetView.snp.makeConstraints { (make) in
            make.leading.trailing.equalTo(self.contentView)
            make.top.equalTo(originalView.snp.bottom)

        }
        
        commonView.snp.makeConstraints { (make) in
            make.leading.trailing.equalTo(self.contentView)
            commonViewTopConstraint = make.top.equalTo(retWeetView.snp.bottom).constraint
            make.height.equalTo(35)
            make.bottom.equalTo(self.contentView)
        }
        //取消选中
        selectionStyle = .none
    }
    //数据模型
    var originalVM: YYLOriginalViewModel? {
        didSet{
            originalView.originalVM = originalVM
            commonView.originalVM = originalVM
            
            //1.先把之前评论栏的top约束删除掉，再根据数据重新创建顶部约束，要保证评论栏只有一个顶部约束
            commonViewTopConstraint?.deactivate()
            
            if originalVM?.homeModel?.retweeted_status != nil{//如果有转载微博
                retWeetView.isHidden = false
                commonView.snp.makeConstraints({ (make) in
                    //重新设置top约束
                    commonViewTopConstraint = make.top.equalTo(retWeetView.snp.bottom).constraint
                })
            retWeetView.originalVM = originalVM
            
            
            } else {  //如果没有转载微博
                retWeetView.isHidden = true
                commonView.snp.makeConstraints({ (make) in
                    //重新设置top约束
                    commonViewTopConstraint = make.top.equalTo(originalView.snp.bottom).constraint
                })

            }
        }

    }
    
    //MARK:------懒加载各种属性
    //原始微博视图
    lazy var originalView: YYLHomeOriginalView = YYLHomeOriginalView()
    //评论视图
    lazy var commonView: YYLHomeCommonView = YYLHomeCommonView()
    //分享微博视图
    lazy var retWeetView: YYLHomeRetWeetView = YYLHomeRetWeetView()
    
    //记录评论视图的顶部约束
    var commonViewTopConstraint: Constraint?
    
    
    
}
